package com.example.qaplatform.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import com.example.qaplatform.entity.SpotEntity;
import com.example.qaplatform.repository.SpotRepository;
import com.example.qaplatform.service.RagflowService;

@RestController
@RequestMapping("/api/spot")
public class SpotController {

    @Autowired
    private SpotRepository spotRepository;

    @Autowired
    private RagflowService ragflowService;

    // 首页/列表页：分页+类别筛选
    @GetMapping("/list")
    public Map<String, Object> listSpots(@RequestParam(defaultValue = "1") int page,
                                         @RequestParam(defaultValue = "12") int size,
                                         @RequestParam(required = false) String category) {
        List<SpotEntity> allSpots = spotRepository.findAll();
        // 类别筛选
        List<SpotEntity> filtered = new ArrayList<>();
        if (category == null || category.isEmpty()) {
            filtered = allSpots;
        } else {
            for (SpotEntity spot : allSpots) {
                if (category.equals(spot.getCategory())) {
                    filtered.add(spot);
                }
            }
        }
        int total = filtered.size();
        int from = Math.max(0, (page - 1) * size);
        int to = Math.min(from + size, total);
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (int i = from; i < to; i++) {
            SpotEntity spot = filtered.get(i);
            String name = spot.getName();
            String imageUrl = ragflowService.getImageUrl(name);
            Map<String, Object> map = new HashMap<>();
            map.put("name", name);
            map.put("category", spot.getCategory());
            map.put("imageUrl", imageUrl);
            resultList.add(map);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("list", resultList);
        return result;
    }

    // 搜索接口
    @GetMapping("/search")
    public Map<String, Object> searchSpot(@RequestParam String name) {
        Optional<SpotEntity> spotOpt = spotRepository.findByName(name);
        Map<String, Object> result = new HashMap<>();
        String imageUrl = ragflowService.getImageUrl(name);
        String desc = ragflowService.getDesc(name);
        System.out.println("[SpotController] 搜索: 名称=" + name + ", imageUrl=" + imageUrl + ", desc=" + desc);
        if (spotOpt.isPresent()) {
            SpotEntity spot = spotOpt.get();
            result.put("name", spot.getName());
            result.put("category", spot.getCategory());
            result.put("imageUrl", imageUrl);
            result.put("desc", desc);
        } else {
            result.put("name", name);
            result.put("category", "");
            result.put("imageUrl", imageUrl);
            result.put("desc", desc);
        }
        return result;
    }

    // 新增：详情页/弹窗，单独查desc
    @GetMapping("/desc")
    public Map<String, Object> getSpotDesc(@RequestParam String name) {
        String desc = ragflowService.getDesc(name);
        Map<String, Object> result = new HashMap<>();
        result.put("name", name);
        result.put("desc", desc);
        return result;
    }
} 